package com.example.microinvestmarket.service.impl;
import com.example.microinvestcommon.dto.StockSymbolAndDataDto;
import com.example.microinvestcommon.pojo.Result;
import com.example.microinvestcommon.util.ChangeListAndJsonUtil;
import com.example.microinvestcommon.util.StockPriceUtil;
import com.example.microinvestmarket.dao.CreateStockMapper;
import com.example.microinvestmarket.service.StockCountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class StockCountServiceImpl implements StockCountService {
    private StockPriceUtil stockPriceUtil;
    private  ChangeListAndJsonUtil changeListToJsonUtil;
    private final CreateStockMapper createStockMapper;
    private static final Logger logger = LoggerFactory.getLogger(StockCountServiceImpl.class);

    private static final int TOTALTURNS=48;
    private static final int PRICEPERTURN=10;

    @Autowired
    public StockCountServiceImpl(CreateStockMapper createStockMapper) {
        this.createStockMapper = createStockMapper;
    }
    @Override
    @Transactional
    public Result<String> createStock() {
        try {
            // 1. 获取所有股票数据
            StockSymbolAndDataDto[] stockDtos = createStockMapper.getStocksAllData();

            // 2. 处理每个股票数据
            for (StockSymbolAndDataDto dto : stockDtos) {
                // JSON -> List
                List<List<Double>> priceHistory = changeListToJsonUtil.changeToList(dto.getStockData());

                // 取最后一组最后一个价格
                Double lastPrice = priceHistory.get(priceHistory.size() - 1)
                        .get(priceHistory.get(priceHistory.size() - 1).size() - 1);

                // 生成新一天价格数据
                List<Double> newDayData = StockPriceUtil.createStockPrices(lastPrice, TOTALTURNS, PRICEPERTURN);
                priceHistory.add(newDayData);

                // 更新 DTO
                dto.setStockListData(priceHistory);
                dto.setStockData(convertToListJson(priceHistory));

                // 更新数据库
                try {
                    createStockMapper.updateStockData(dto.getStockSymbol(), dto.getStockData());
                } catch (Exception dbEx) {
                    logger.error("更新股票 {} 数据失败: {}", dto.getStockSymbol(), dbEx.getMessage(), dbEx);
                }

                logger.info("股票 {} 更新后的数据: {}", dto.getStockSymbol(), dto.getStockData());
            }

            return Result.success("股票数据更新完成，共处理 " + stockDtos.length + " 条记录。");

        } catch (Exception e) {
            logger.error("createStock 发生异常: {}", e.getMessage(), e);
            return Result.error("股票数据更新失败: " + e.getMessage());
        }
    }


    @Override
    public Result<List<Double>> getStockMessage(String stockName,int key) throws Exception {
        String stockJson = createStockMapper.getStockMessage(stockName);
        List<Double> stockDataList = changeListToJsonUtil.changeToListOneDim(stockJson);
        // 检查 key 是否越界
        if (key < 0 || key >= stockDataList.size()) {
            return Result.error("索引超出范围");
        }

        // 获取指定的子列表
//        List<Double> targetList = stockDataList.get(key);
//
//        return Result.success(targetList);
        return Result.success(stockDataList);
    }


    private String convertToListJson(List<List<Double>> stockData){
        return stockData.toString();
    }
}
